<div class="container-fluid">
    <div class="wrapper">
        <div class="panel" id="nav">
            <div class="panel-heading"><h3 class="panel-title">不良品处理</h3></div>
            <div class="panel-body">
                <form action="__URL__/bad_products_handle" method="get">
                    <div class="container-fluid">
                        <div class="col-sm-12 col-md-3  add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">采购单号</span>
                                <input class="form-control" type="text" name="purchaseorder_id">
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-3  add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">供应商</span>
                                <input type="text" name="supplier" class="form-control" value="">
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-3  add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">SKU</span>
                                <input class="form-control" type="text" name="sku">
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-3  add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">采购员</span>
                                <input type="text" name="purchase_man" class="form-control" value="">
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-6  add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">质检时间</span>
                                <input class="form-control form-date" type="text" name="check_time_from"
                                       style="cursor:pointer;" readonly>
                                <span class="input-group-addon"><span class="icon-calendar"></span></span>
                                <input class="form-control form-date" type="text" name="check_time_to"
                                       style="cursor:pointer;" readonly>
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-3  add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">状态</span>
                                <select class="chosen-select form-control" tabindex="-1" name="status"
                                        data-name-group="common">
                                    <option value="" class="empty-opt">-- 请选择 --</option>
                                    <volist name="status" id="item">
                                        <option value="{$key}">{$item}</option>
                                    </volist>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3 add-space">
                            <input class="btn btn-primary" type="submit" value="查询">
                            <input class="btn btn-primary" type="button" onclick="reset()" value="重置">
                            <!--<input class="btn btn-primary" type="submit" name="downcsv" value="下载">-->
                        </div>
                    </div>
                </form>
                <div class="clearfix"></div>
                <hr>
                <table class="table" cellspacing="0" width="100%">
                    <tbody>
                    <volist name="result" id="val">
                        <tr style="background-color:darkgray">
                            <th style="width:80px;">序列</th>
                            <th style="width:100px;">采购员</th>
                            <th style="width:150px;">采购单号</th>
                            <th style="width:250px;">质检明细单号</th>
                            <th style="width:110px;">SKU</th>
                            <th style="width:200px;">中文名称</th>
                            <th style="width:190px;">供应商</th>
                        </tr>
                        <tr>
                            <td>{$key+1}</td>
                            <td>{$val.purchase_man}</td>
                            <td>{$val.purchaseorder_id}</td>
                            <td>{$val.check_detail_id}</td>
                            <td>{$val.sku}</td>
                            <td>{$val.sku_name}</td>
                            <td>{$val.supplier}</td>
                        </tr>
                        <tr style="background-color:rgba(129,208,177,.3);">
                            <th>处理人</th>
                            <th>质检人</th>
                            <th>质检时间</th>
                            <th>质检不合格原因</th>
                            <th>状态</th>
                            <th>不合格数量</th>
                            <th>详情</th>
                            <th></th>
                        </tr>
                        <tr style="background-color:#ffffff;">


                            <td>{$val.operate_man}</td>
                            <td>{$val.quality_man}</td>
                            <td>{$val.check_time}</td>
                            <td>{$val.quality_remark}</td>
                            <td>{$val.status}</td>
                            <td>{$val.quantity}</td>
                            <td>
                                <input type="hidden" value="{$val.purchaseorder_id}" id="purchaseorder_id_{$val.id}">
                                <input type="hidden" value="{$val.sku}" id="sku_{$val.id}">
                                <input type="hidden" value="{$val.id}" id="unqualified_id_{$val.id}">
                                <input type="hidden" value="{$val.sku_name}" id="sku_name_{$val.id}">
                                <input type="hidden" value="{$val.check_detail_id}" id="check_detail_id_{$val.id}">
                                <input type="hidden" value="{$val.store}" id="store_{$val.id}">
                                <if condition="$val.status eq '未处理'">
                                <input value="详情" class="btn btn-info btn-sm" onclick="chk_tr_display('a_{$val.id}','b_{$val.id}')"
                                       id="b_{$val.id}" type="button">
                                </if>
                            </td>
                            <td></td>
                        </tr>
                        <tr style="display:none" id="a_{$val.id}">
                            <td colspan="11">
                                <table class="xiangqing table table-striped  table-hover">
                                    <tr>
                                        <th>序号</th>
                                        <th id="qt_25227002">数量 </th>
                                        <th style="width:140px;">处理方式</th>
                                        <th>采购处理备注</th>
                                        <th>运费</th>
                                        <th>单价</th>
                                        <th>采购处理日期</th>
                                        <th>管理员处理结果</th>
                                        <th>
                                            <button class="btn btn-info addproduct btn-sm" type="button"
                                                    id="addproduct_{$val.id}">添加处理条数
                                            </button>
                                        </th>
                                    </tr>
                                    <tbody id="detail_{$val.id}">

                                    <?php if(!empty($val['bad_products'])){ ?>
                                    <volist name="val.bad_products" id="v">
                                    <tr>
                                        <td>{$key+1}</td>
                                        <td>
                                        <input value="{$v.id}" type="hidden">
                                        <input name="quantity[]" onkeyup="this.value=this.value.replace(/\D/g,'')"
                                        onafterpaste="this.value=this.value.replace(/\D/g,'')"
                                        value="{$v.quantity}" type="text" class="w100 form-control"></td>
                                        <td><select class="span2 form-control" name="fail_deal_way[]">
                                        <option value="" class="empty-opt">不合格处理方式</option>
                                        <volist name="way" id="item">
                                        <option value="{$key}" <if condition="$v.fail_deal_way eq $key"> selected=selected</if>>{$item}</option>
                                        </volist>
                                        </select></td>

                                        <td><input name="unqualified_reason[]" value="{$v.unqualified_reason}"
                                        type="text" class="w100 form-control"></td>
                                        <td><input name="return_cost[]" value="{$v.return_cost}" type="text" class="w100 form-control">
                                        </td>
                                        <td><input name="coupon_price[]" value="{$v.coupon_price}" type="text" class="w100 form-control"></td>

                                        <td>{$v.deal_time}</td>
                                        <td></td>
                                        <td></td>
                                    </tr>
                                    </volist>
                                    <?php }else{ ?>
                                    <tr>
                                        <td>1</td>
                                        <td>
                                            <input value="0" id="bad_{$val.id}" type="hidden">
                                            <input name="quantity[]" onkeyup="this.value=this.value.replace(/\D/g,'')"
                                                   onafterpaste="this.value=this.value.replace(/\D/g,'')"
                                                   id="coun_{$val.id}" value="" type="text" class="w100 form-control"></td>
                                        <!-- 处理方式 -->
                                        <td><select id="way_{$val.id}" class="span2 form-control" name="fail_deal_way[]">
                                            <option value="" class="empty-opt">不合格处理方式</option>
                                            <volist name="way" id="item">
                                                <option value="{$key}">{$item}</option>
                                            </volist>
                                        </select></td>

                                        <td><input id="remark_{$val.id}" name="unqualified_reason[]" value=""
                                                   type="text" class="w100 form-control"></td>
                                        <td><input id="return_cost_{$val.id}" name="return_cost[]" value="" type="text" class="w100 form-control">
                                        </td>
                                        <td><input id="coupon_price_{$val.id}" name="coupon_price[]" value="" type="text" class="w100 form-control"></td>

                                        <td></td>
                                        <td></td>
                                        <td></td>
                                    </tr>
                                    <?php } ?>
                                    </tbody>
                                    <tr>
                                        <td colspan="9">
                                            <span id="co_{$val.id}" style="display:none;">1</span>
                                            <input value="提交" id="submit_{$val.id}" class="tijiao btn btn-info btn-sm" data-id="{$val.id}" type="button">
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </volist>
                  </tbody>
                </table>

                <div class="clearfix clear"></div>
                <div class="row">
                    <div class="col-md-6 pull-left">
                        <label>每页 20 条记录
                            显示 1 到 20 项，共 {$count}项</label>
                    </div>
                    <div class="col-md-6">
                        <div class="page pull-right" style="padding: 0;margin: 0">
                            {$page}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(function () {
        $("input[name='purchaseorder_id']").val("<?php  echo $_GET['purchaseorder_id'];?>");
        $("input[name='supplier']").val("<?php echo $_GET['supplier'];?>");
        $("input[name='sku']").val("<?php echo $_GET['sku'];?>");
        $("input[name='purchase_man']").val("<?php  echo $_GET['purchase_man'];?>");
        $("input[name='check_time_from']").val("<?php  echo $_GET['check_time_from'];?>");
        $("input[name='check_time_to']").val("<?php echo $_GET['check_time_to'];?>");
        $("select[name='status']").val("<?php echo $_GET['status'];?>");
    });
    function chk_tr_display(tr,a)
    {
        if(document.getElementById(tr).style.display=='none')
        {
            document.getElementById(tr).style.display='';
            document.getElementById(a).innerHTML='收起';
        }
        else
        {
            document.getElementById(tr).style.display='none';
            document.getElementById(a).innerHTML='展开';
        }
    }

    /**
     * 重置
     * */
    function reset() {
        $('#form_search').find('input').val('');
        $('#form_search').find('select').val('');
    }
    /**
     * 填充编辑数据
     * khq 2017.3.2
     */
    $(".edit_detail").click(function () {
        var id = $(this).attr('data-id');
        if (id) {
            $.get("Warehouse/BadProducts/ajax_select_detail",
                    {id: id},
                    function (data) {
                        var obj = jQuery.parseJSON(data);
                        if (obj.status == 'Y') {
                            $(".sku").html(obj.message.sku);
                            $("input[name=id_edit]").val(id);
                            $(".sku_name").html(obj.message.sku_name);
                            $(".quantity").html(obj.message.quantity);
                            $(".quality_remark").val(obj.message.quality_remark);
                        } else {
                            layer.msg(obj.message, {icon: 5});
                        }
                    });
        }
    });
    /**
     * 编辑明细
     * khq 2017.1.13
     */
    function edit() {
        var id = $("input[name=id_edit]").val();
        var quality_remark = $("input[name=quality_remark]").val();
        $.get("Warehouse/BadProducts//ajax_edit",
                {
                    id: id,
                    quality_remark: quality_remark
                },
                function (data) {
                    if (data.status == 0) layer.msg(data.info);
                    var obj = jQuery.parseJSON(data);
                    if (obj.status == 'Y') {
                        layer.msg(obj.message, {icon: 6});
                        window.location.reload();
                    } else {
                        layer.msg(obj.message, {icon: 5});
                    }
                });
    }

    /**
     * @returns {string}
     * 获取搜索条件
     * khq 2017.3.2
     */
    function search_params() {
        var param = '';
        var purchaseorder_id = $("input[name='purchaseorder_id']").val();
        var supplier = $("input[name='supplier']").val();
        var sku = $("input[name='sku']").val();
        var purchase_man = $("input[name='purchase_man']").val();
        var check_time_from = $("input[name='check_time_from']").val();
        var check_time_to = $("input[name='check_time_to']").val();
        var status = $("select[name='status']").val();

        if ($.trim(purchaseorder_id) != '') {
            param += "&purchaseorder_id=" + $.trim(purchaseorder_id);
        }
        if ($.trim(supplier) != '') {
            param += "&supplier=" + $.trim(supplier);
        }
        if ($.trim(sku) != '') {
            param += "&sku=" + $.trim(sku);
        }
        if ($.trim(purchase_man) != '') {
            param += "&purchase_man=" + $.trim(purchase_man);
        }
        if ($.trim(status) != '') {
            param += "&status=" + $.trim(status);
        }
        if ($.trim(check_time_from) != '' && $.trim(check_time_to) != '') {
            param += "&check_time_from=" + $.trim(check_time_from);
            param += "&check_time_to=" + $.trim(check_time_to);
        }
        return param;
    }

    $(".addproduct").click(function(){
        ids=$(this).attr('id');
        var lengs=ids.length;
        var id=ids.substr(11,lengs-11);             //产品的ID
        var shu=parseInt($("#quantity_"+id).val()); //不合格数量
        var co = parseInt($("#co_"+id).text());     //添加条数的序号
        //alert(co);
        if(co >= shu)
        {
            $(this).css('display','none');
        }
        else{
            var count_id=id+'_'+parseInt(co+1001);

            var table = '<tr id="tr_'+count_id+'">';
            table +='<td>'+parseInt(co+1)+'</td>';
            table += '<td><input type="hidden" style="width:100px;" value="0" id="bad_'+count_id+'"/>' +
                    '<input style="width:100px;" type="text" placeholder="" onkeyup="this.value=this.value.replace(/\\D/g,\'\')" ' +
                    'onafterpaste="this.value=this.value.replace(/\\D/g,\'\')"  name="count[]" value="" id="coun_'+count_id+'" class="w100 form-control"/></td>';
            table += '<td><select style="width:130px;" class="span2" id="way_'+count_id+'"  name="fail_deal_way[]">';
            table +='<option value="1000" >不合格处理方式</option>';
            table += '<?php foreach($way as $ks=>$v){ ?><option value="<?php echo $ks;?>" ><?php echo $v;?></option><?php }?>';
            table +='</select></td>';
            table += '<td><input type="text" style="width:100px;" id="remark_'+count_id+'" value="" name="unqualified_reason[]" class="w100 form-control"/></td>';
            table += '<td><input type="text" id="return_cost_'+count_id+'" style="width:100px;"   name="coupon_price[]" value="" class="w100 form-control"/></td>';
            table +=  ' <td><input type="text" id="coupon_price_'+count_id+'" style="width:100px;" name="return_cost[]" value="" class="w100 form-control"/></td>';
            $("#co_"+id).text(co+=1);
            $("#detail_"+id).append(table);

        }
    });
    $(".tijiao").click(function(){
        var id = $(this).attr("data-id");
        var bad_id       = '';
        var quantity     = '';
        var way          = '';
        var remark       = '';
        var return_cost  = '';
        var single_price = '';
        var sku              = $("#sku_"+id).val();
        var store            = $("#store_"+id).val();
        var sku_name         = $("#sku_name_"+id).val();
        var check_detail_id  = $("#check_detail_id_"+id).val();
        var purchaseorder_id = $("#purchaseorder_id_"+id).val();
        $("#detail_"+id).find("tr").each(function(){
            bad_id       += $(this).find("input").eq(0).val()+ ";";           //不良品出库id
            quantity     += $(this).find("input").eq(1).val()+ ";";           //数量
            way          += $(this).find("select").eq(0).val()+ ";";          //处理方式
            remark       += $(this).find("input").eq(2).val()+ ";";           //采购处理备注
            return_cost  += $(this).find("input").eq(3).val()+ ";";           //运费
            single_price += $(this).find("input").eq(4).val()+ ";";           //单价
        });
        $.post("Warehouse/BadProducts/deal",
                {   'id':id,
                    'bad_id'  :bad_id,
                    'quantity':quantity,
                    'way':way,
                    'remark':remark,
                    'sku' : sku,
                    'store' : store,
                    'sku_name' : sku_name,
                    'check_detail_id' : check_detail_id,
                    'purchaseorder_id': purchaseorder_id,
                    'return_cost':return_cost,
                    'single_price':single_price
                },
                function (data) {
                    if(data.status==0) layer.msg(data.info);
                    var obj = jQuery.parseJSON(data);
                    if (obj.status == 'Y') {
                        layer.msg(obj.message, {icon: 6});
                        window.location.reload();
                    } else {
                        layer.msg(obj.message, {icon: 5});
                    }
                });
    });
    function download_csv() {
        var param = search_params();
        window.location.href = '__URL__/index?downcsv=1' + param;
    }

</script>
